package preferences;

/**
 * Preferences - Allows the user program the visualisation with personalised information
 * @author - Owen Phelan
 */

import weeks.GCalendarServices;
import weeks.GBloggerServices;
import bluetooth.BluetoothSpotter;
import bluetooth.DeviceDetails;
import weeks.weeks;
import processing.core.PApplet;
import processing.core.PImage;

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Properties;

import com.google.gdata.util.AuthenticationException;

import controlP5.*;

public class Splash extends PApplet {

	public static void main(String[] args) {
		//PApplet.main(new String[] { "--present", "Weeks - Splash" });
	}

	ControlP5 controlP5;

	//Preferences p = new Preferences();
	
	PImage back;
	PImage mycharacter = loadImage("Weeks/data/1.png");
	PImage characterTwo = loadImage("Weeks/data/2.png");
	PImage characterThree = loadImage("Weeks/data/3.png");
	PImage characterFour = loadImage("Weeks/data/4.png");
	
	String user;
	String pass;
	String url;
	String port;
	String rdf;
	String luas;
	String calendar;
	String proxy;
	String bonjour;
	boolean bluetooth;
	
	Toggle enableProxy;
	Toggle enableBlogger;
	Toggle enableLuas;
	Toggle enableBonjour;
	Toggle enablebluetooth;
	
	Textfield Username;
	Textfield Password;
	Textfield ProxyURL;
	Textfield ProxyPort;
	Textfield RDFURL;
	
	Textarea message;
	Textarea googleservicesmessage;
	
	ScrollList discoveryList;
	
	Radio Events;
	Radio LuasLines;
	
	Textlabel header;

	public Splash(){
		this.init();
	}
	
	public void setup() {
		  size(800, 480);
		  frameRate(20);
		  
		  controlP5 = new ControlP5(this);
		  
		  header = new Textlabel(this,"THE VISUAL CALENDAR",20,40,400,400,0xFFFFFF,ControlP5.synt24);
		  header.setLetterSpacing(4);
		  
		  controlP5.tab("default").setLabel("Home");
		  controlP5.tab("default").activateEvent(true);
		  controlP5.tab("default").setId(1);
		  
		  controlP5.tab("google").setLabel("Google Services");
		  controlP5.tab("google").activateEvent(true);
		  controlP5.tab("google").setId(2);
		  
		  controlP5.tab("local").setLabel("Local Data");
		  controlP5.tab("local").activateEvent(true);
		  controlP5.tab("local").setId(3);
		  
		  controlP5.tab("luas").setLabel("LUAS - RDF");
		  controlP5.tab("luas").activateEvent(true);
		  controlP5.tab("luas").setId(4);
		  
		  controlP5.tab("network").setLabel("Network Settings");
		  controlP5.tab("network").activateEvent(true);
		  controlP5.tab("network").setId(5);
		  
		  
		  //Home Tab
		  message = controlP5.addTextarea(
				     "output", 
				     "This is an ambient display that conveys data gathered from the web and your local network. Please specify your preferences using this applet", 100,100,200,100);
		  
		  //Google ID + Login
		  Username = controlP5.addTextfield("Username",20,150,200,20);
		  Username.moveTo("google");
		  Password =  controlP5.addTextfield("Password",250,150,200,20);
		  Password.moveTo("google");
		  
		  controlP5.addButton("Login", 1,500,150,60,20).setId(3);
		  LoginListener lol = new LoginListener();
		  controlP5.controller("Login").addListener(lol);
		  controlP5.controller("Login").moveTo("google");
		  
//		  Textarea output = controlP5.addTextarea(
//				     "output", 
//				     System.out.toString(), 20,height-60,400,100);
		  
		  //Network
		  ProxyListener pl = new ProxyListener();
		  enableProxy = controlP5.addToggle("Use Proxy",20,150,60,20);
		  enableProxy.moveTo("network");		  
		  enableProxy.setState(true);
		  controlP5.controller("Use Proxy").addListener(pl);
		  ProxyURL =  controlP5.addTextfield("Proxy URL",100,150,200,20);
		  ProxyURL.moveTo("network");
		  ProxyPort = controlP5.addTextfield("Proxy Port Number",330,150,200,20);
		  ProxyPort.moveTo("network");
		  ProxyURL.setVisible(true);
		  ProxyURL.setValue("proxy4.ucd.ie");
		  ProxyPort.setVisible(true);
		  ProxyPort.setValue("8484");
		  
		  //Local Data
		  enablebluetooth = controlP5.addToggle("Use bluetooth",20,150,60,20);
		  enablebluetooth.moveTo("local");
		  BluetoothListener ul = new BluetoothListener();
		  controlP5.controller("Use bluetooth").addListener(ul);
		  
		  discoveryList = controlP5.addScrollList("Bluetooth discovery list",100,100,120,280);
		  
		  enableBonjour = controlP5.addToggle("Use Bonjour Discovery",100,150,60,20);
		  enableBonjour.moveTo("local");
		  BonjourListener bl = new BonjourListener();
		  controlP5.controller("Use Bonjour Discovery").addListener(bl);
		  
		  //image(mycharacter, 100, 220);
		  //image(characterTwo, 250, 220);
		  //image(characterThree, 400, 220);
		  
		  //LUAS
		  LUASListener ll = new LUASListener();
		  enableLuas = controlP5.addToggle("Use LUAS",20,150,60,20);
		  enableLuas.moveTo("luas");
		  controlP5.controller("Use LUAS").addListener(ll);
		  RDFURL =  controlP5.addTextfield("LUAS RDF URL",100,150,200,20);
		  RDFURL.moveTo("luas");
	      RDFURL.setVisible(false);

		  //Save
		  controlP5.addButton("OK", 1,width-100,height-60,60,20).setId(1);
		  OKListener okl = new OKListener();
		  controlP5.controller("OK").addListener(okl);
		  controlP5.controller("OK").moveTo("global");
		  
		  //Cancel
		  controlP5.addButton("Cancel", 1,width-180,height-60,60,20).setId(1);
		  CancelListener c = new CancelListener();
		  controlP5.controller("Cancel").addListener(c);
		  controlP5.controller("Cancel").moveTo("global");
		  
		}
	
	public void draw() {
		  background(800);
		  header.draw(this);
	}
	
	class ProxyListener implements ControlListener {
		int col;
		  public void controlEvent(ControlEvent theEvent) {
		    col = (int)theEvent.controller().value();
		    if(col == 1){
		    	System.out.println("Proxy Enabled");
		    	ProxyURL.setVisible(true);
		    	ProxyPort.setVisible(true);
				//System.setProperty("https.proxyHost", ProxyURL.getText());
	    		//System.setProperty("https.proxyPort", ProxyPort.getText());
		    }else{
		    	System.out.println("Proxy Disabled");
		    	ProxyURL.setVisible(false);
		    	ProxyPort.setVisible(false);
		    	//System.setProperty("https.proxyHost", "");
	    		//System.setProperty("https.proxyPort", "");
		    }
		  }
	}
	
	
	class BonjourListener implements ControlListener {
		int col;
		  public void controlEvent(ControlEvent theEvent) {
		    col = (int)theEvent.controller().value();
		    if(col == 1){
		    	System.out.println("Bonjour Enabled");
		    	bonjour = "on";
		    }else{
		    	System.out.println("Bonjour Disabled");
		    	bonjour = "off";
		    }
		  }
	}
	
	class BluetoothListener implements ControlListener {
		int col;
		public void controlEvent(ControlEvent theEvent) {
			col = (int)theEvent.controller().value();
		    if(col == 1){
		    	System.out.println("Bluetooth Scanning Enabled");
		    	bluetooth = true;
		    }else{
		    	System.out.println("Bluetooth Scanning Disabled");
		    	bluetooth = false;
		    }
		  }
	}
	
	//Enable Luas Listener
	class LUASListener implements ControlListener {
		int col;
		public void controlEvent(ControlEvent theEvent) {
			col = (int)theEvent.controller().value();
		    if(col == 1){
		    	System.out.println("Luas RDF Enabled");
		    	rdf = "on";
		    	RDFURL.setVisible(true);
		    	RDFURL.setVisible(true);
		    }else{
		    	System.out.println("Luas RDF Disabled");
		    	rdf = "off";
		    	RDFURL.setVisible(false);
		    	RDFURL.setVisible(false);
		    }
		  }
	}

	
	//Login Listener
	class LoginListener implements ControlListener {
		@SuppressWarnings("unused")
		private GCalendarServices gcs;
		@SuppressWarnings("unused")
		private GBloggerServices gbs;
		@SuppressWarnings("unused")
		private BluetoothSpotter bts;

		public void controlEvent(ControlEvent theEvent) {
			  //System.setProperty("https.proxyHost", ProxyURL.getText());
			  //System.setProperty("https.proxyPort", ProxyPort.getText());
			  try {
				gcs = new GCalendarServices();
				gbs = new GBloggerServices();
				bts = new BluetoothSpotter();
				
				GCalendarServices.calendarServices(Username.getText(), Password.getText());
				GBloggerServices.bloggerServices(Username.getText(), Password.getText());
				
				
		        List<DeviceDetails> devices = bts.performScan();
		        while(bts.scanInProgress){
					System.out.println("Scanning Bluetooth Range");
				}
		        // Iterate through all detected devices and print details to the console
		        for (DeviceDetails device : devices) {
		           System.out.println(device.getDeviceAddress() + " - " + device.getDeviceName());
		        }
				
				weeks w = new weeks(gcs.getNumberOfCals(), gcs.getNumberOfEvents(), Username.getText(), bluetooth);
				w.init();
				
				Frame frame = new Frame("Weeks");
				frame.setSize(screen.width, screen.height);
				frame.add(w);		
				frame.setVisible(true);
			} catch (AuthenticationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			  
		  }	
	}
	
	//Cancel Listener
	class CancelListener implements ControlListener {
		public void controlEvent(ControlEvent theEvent) {
			System.out.println("Aborted");
			exit();
			System.exit(0);
		}
	}
	
	//OK Button Listener
	class OKListener implements ControlListener {

		  public void controlEvent(ControlEvent theEvent) {
			  
			  //exit();
			  //System.exit(0);
		  }
	}
	
	//Save the preferences
	public void savePreferences(){
		
		Properties p = new Properties();
		
		p.setProperty("user", user);
		p.setProperty("pass", pass);
		p.setProperty("url", url);
		p.setProperty("proxy",proxy);
		
		//create file to save to
		File save=new File("Weeks/data/preferences.txt");
		OutputStream pos=null;
			
		//Open a stream, catch exceptions
		try{		
			pos=new FileOutputStream(save);		
		}catch(IOException ioe){
			System.err.println("Uh Oh - Problems");
		}
			
		//save properties to file	
		try{
			p.store(pos,"Properties - Weeks Visual Calendar");
		}catch(Exception ioe){
			System.err.println("Uh Oh - Problems");
		}	
	}

	//Load the preferences
	public void loadPreferences(){
		
		File inFile = new File("Weeks/data/preferences.txt");
		FileInputStream in;

		Properties p = new Properties();
		
		try
		{
		    in = new FileInputStream(inFile);
		    p.load(in);
		}
		
		catch(java.io.IOException ioe){
			System.err.println("Uh Oh. Property load error");
		}
		
		String loaded = null;
		
		loaded = p.getProperty("user");
		this.user = loaded;
		loaded = p.getProperty("pass");
		this.pass = loaded;
		loaded = p.getProperty("url");
		this.url = loaded;
		loaded = p.getProperty("proxy");
		this.proxy = loaded;
		
	}
}